package cn.house.houseai.controller.user;

import cn.house.houseai.constant.JwtClaimsConstant;
import cn.house.houseai.entify.ChangePasswordRequest;
import cn.house.houseai.entify.Users;
import cn.house.houseai.properties.JwtProperties;
import cn.house.houseai.service.UserService;
import cn.house.houseai.tools.JwtUtil;
import cn.house.houseai.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 羡y
 * @date 2024/9/27
 * @Description 用户相关接口
 */
@RestController("UserController")
@RequestMapping("/user")
@Api(tags = "C端-用户相关接口")
@Slf4j
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    // 验证密码
    @GetMapping("/{user_id}")
    @ApiOperation("根据ID查询用户数据")
    public ResultVo getByMessage(@PathVariable Integer user_id) {
        try {
            ResultVo user = userService.getByMessage(user_id);
            if (user != null) {
                return new ResultVo(100, user.getData(), "查询成功"); // 确保这里返回的是 user 对象本身
            } else {
                return new ResultVo(101, null, "查询失败");
            }
        } catch (Exception e) {
            return new ResultVo(100, null, "查询失败..请稍后重试");
        }
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "根据用户名和密码进行用户登录")
    public ResultVo login(@RequestParam String username, @RequestParam String password) {
        // 调用 UserService 的登录方法
        ResultVo result = userService.login(username, password);

        return result; // 直接返回服务层的结果
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public ResultVo register(@RequestBody Users user){
        return userService.register(user);
    }


    @PutMapping("/update/{user_id}")
    @ApiOperation(value = "用户修改信息")
    public ResponseEntity<ResultVo> updateUser(@PathVariable int user_id, @RequestBody Users user) {
        ResultVo result = userService.updateUser(user_id, user);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("用户修改密码")
    @PutMapping("/change-password/{user_id}")
    public ResultVo changePassword(@PathVariable int user_id, @RequestBody ChangePasswordRequest request) {
        // 直接调用服务层的修改密码方法
        ResultVo result = userService.updatePassword(user_id, request);
        return result; // 返回服务层的结果
    }





}
